package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.CollectionKit;
import com.wxxymaker.edu.model.entity.Course;
import com.wxxymaker.edu.model.entity.JoinTestPaper;
import com.wxxymaker.edu.model.entity.TestPaper;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.core.Joins;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/10/12 19:45
 * @DESCRIBE
 */
@Bean
public class JoinTestPaperService implements Service<JoinTestPaper> {

    @Inject
    private TestPaperService testPaperService;

    public Page<JoinTestPaper> page(Integer kid, Integer uid, Integer teaId, Integer status,
                                    boolean joinCourse, boolean joinUser, Integer page, Integer size) {
        if (uid == null && teaId == null) return null;
        AnimaQuery<JoinTestPaper> query = Anima.select().from(JoinTestPaper.class)
                .join(Joins.with(TestPaper.class)
                        .as(JoinTestPaper::getTestpaper)
                        .on(JoinTestPaper::getTestPaperId, TestPaper::getId))
                .order(JoinTestPaper::getTime, OrderBy.DESC);
        if (joinCourse) query.join(Joins.with(Course.class)
                .as(JoinTestPaper::getCourse)
                .on(JoinTestPaper::getCourseId, Course::getId));
        if (joinUser) query.join(Joins.with(User.class)
                .as(JoinTestPaper::getUser)
                .on(JoinTestPaper::getUserId, User::getId));
        if (kid != null) query.where(JoinTestPaper::getCourseId, kid);
        if (uid != null) query.where(JoinTestPaper::getUserId, uid);
        if (teaId != null) query.where(JoinTestPaper::getTeaId, teaId);
        if (status != null) query.where(JoinTestPaper::getStatus, status);
        Page<JoinTestPaper> page1 = query.page(page, size);
        if (CollectionKit.isNotEmpty(page1.getRows())) {
            page1.getRows().forEach(joinTestPaper -> {
                if (!testPaperService.canDownResult(joinTestPaper.getUserId(), joinTestPaper.getCourseId(),
                        joinTestPaper.getTestpaper())) {
                    joinTestPaper.getTestpaper().setResult(null);
                }
            });
            return page1;
        }
        return null;
    }

    @Override
    public JoinTestPaper add(JoinTestPaper joinTestPaper) {
        joinTestPaper.setTime(TimeUtils.getTime());
        joinTestPaper.setStatus(1);
        Integer integer = joinTestPaper.save().asInt();
        if (integer > 0) {
            joinTestPaper.setId(integer);
            return joinTestPaper;
        }
        return null;
    }

    @Override
    public JoinTestPaper delete(Object pk) {
        JoinTestPaper joinTestPaper = byId(pk);
        if (joinTestPaper!=null) {
            joinTestPaper.delete();
            return joinTestPaper;
        }
        return null;
    }

    @Override
    public boolean change(JoinTestPaper joinTestPaper) {
        if (joinTestPaper.getId()>0) {
            return joinTestPaper.update() > 0;
        }
        return false;
    }

    @Override
    public JoinTestPaper byId(Object pk) {
        return Anima.select().from(JoinTestPaper.class).byId(pk);
    }

    public boolean isJoin(int pid, int uid) {
        return Anima.select().from(JoinTestPaper.class)
                .where(JoinTestPaper::getTestPaperId, pid)
                .where(JoinTestPaper::getUserId, uid)
                .count() > 0;
    }

    public Long countByUid(Integer userId, boolean view) {
        AnimaQuery<JoinTestPaper> where = Anima.select().from(JoinTestPaper.class)
                .where(JoinTestPaper::getUserId, userId);
        if (view) {
            where.gt(JoinTestPaper::getScore, 0);
        }
        return where.count();
    }

    public int deleteByKid(Integer kid) {
        return Anima.delete().from(JoinTestPaper.class)
                .where(JoinTestPaper::getCourseId, kid)
                .execute();
    }
}
